var queues = require('mysql-queues');
const DEBUG = true;

var addLog = function(connection, data, callback) {
	var sql = 'insert into log(itemId, updatePersonId, content, updateDate, type, operation) values(?,?,?,now(),?,?)';
	connection.query(sql, [data.itemId, data.updatePersonId, data.content, data.type, data.operation], function(err, rows){
		if(err) {
			console.log(err);
			console.log(err.stack);
			callback([]);
		}else{
			callback(rows);
			connection.release();
		}
	});
},
selectItemLog = function(connection, itemId,type,callback){
	var sql = "select l.id,l.itemId,l.updatePersonId,date_format(l.updateDate,'%Y-%m-%d %H:%i:%s') as updateDate,l.content,l.`type`,l.operation,e.name from log l,employee e where l.itemId=? and l.type=? and e.id=l.updatePersonId";
	connection.query(sql,[itemId,type], function(err, rows){
		if(err){
			console.log(err);
			console.log(err.stack);
			callback([]);
		}else{
			callback(rows);
			connection.release();
		}
	});
},

selectLog = function(connection, id, callback){
	var sql = 'select * from log where id=?';
	connection.query(sql, id, function(err, rows){
		if(err){
			console.log(err);
			console.log(err.stack);
			callback(null);
		}else{
			callback(rows);
			connection.release();
		}
	});
};

module.exports = {
	addLog : addLog,
	selectItemLog:selectItemLog,
	selectLog : selectLog
};